public class Player {
    //姓名
    String name;
    //生命
    int life;
    //攻击力
    int attack;
    //防御力
    int defense;
    //敏捷，用来判断是否可以躲避
    int agile;
    //等级
    int level;
    //暴击率
    int critRate;
    //暴击伤害
    int critDamage;
    //经验值
    int exp;
    //是否或者
    boolean isLive;
    int maxLife;

    public Player(String name) {

        this.name = name;
        this.maxLife = 100;
        this.life = this.maxLife;
        this.attack = 25;
        this.defense = 10;
        this.agile = 10;
        this.level = 1;
        this.critRate = 25;
        this.critDamage = 150;
        this.exp = 0;
        this.isLive = true;
        Tools.godSay("勇敢的骑士["+name+"]在乱世中诞生了!");
        Tools.playerSay(name+":惩恶扬善!");
    }

    public void show() {
        System.out.println("-----------------------------------------------------------------");
        System.out.println(name+":\t等级:"+this.level+"\t生命:"+this.life+"\t经验:"+this.exp);
        System.out.println("基础属性:\t攻击力:"+this.attack+"\t防御力:"+this.defense+"\t敏捷:"+this.agile);
        System.out.println("特殊属性:\t暴击率:"+this.critRate+"\t暴击伤害:"+this.critDamage);
        System.out.println("------------------------------------------------------------------");
    }

    public void fight(Enemy e) {
        while(this.isLive&&e.isLive) {
//            if(!this.isLive||!e.isLive) {
//                break;
//            }
            //讲口号
            Tools.godSay(this.name+"举起了大剑，砍向"+e.name);
            Tools.playerSay(this.name+":哇哇哇!看招");
            //2、计算损失的生命
            int lostLife = 0;
            //检测是否暴击
            if(Tools.checkRate(this.critRate)) {
                Tools.randomSay(this.name+"哈哈哈，天神给我力量!看我的大招!");
                //发生暴击
                int tt = (int)((this.critDamage/100.0)*this.attack);
                lostLife = tt-e.defense;
            } else {
                //没有发生暴击
                lostLife = this.attack-e.defense;
            }
            if(lostLife<0) {
                lostLife = 1;
            }
            //3、让敌人损失生命
            e.injured(lostLife,this);
            //4、添加经验
            if(!e.isLive) {
                this.exp+=e.giveExp;
                checkUpgrade();
            }
        }

    }

    public void checkUpgrade() {
        if(exp>=Tools.getUpgradeExp(level)) {
            Tools.playerSay(this.name+":啦啦啦啦！天神赐予我力量!我升级啦!");
            this.maxLife+=20;
            this.life = this.maxLife;
            this.attack+=4;
            this.defense+=2;
            this.agile+=2;//敏捷增加2
            this.critRate+=2;
            this.critDamage+=100;
            this.level+=1;
            this.show();
        }
    }

    public void injured(int lostLife) {
        //判断闪避
        if(Tools.checkRate(this.agile)) {
            Tools.randomSay(this.name+":哈哈哈！小样你打不到我!");
            return;
        }
        //1、讲口号
        Tools.playerSay(this.name+":哇哇，居然被反击了,损失了["+lostLife+"]生命");
        //2、丢失生命
        this.life-=lostLife;
        //3、判断是否死亡
        if(this.life<=0) {
            this.isLive = false;
            Tools.playerSay(this.name+":天妒英才啊！天妒英才!");
            Tools.godSay("大侠是否重新来过！");
            return;
        }
    }
}
